package item.inkcloud.studtroom;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import javax.websocket.Session;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class ChatHandler extends TextWebSocketHandler {


    private static HashMap<String, ArrayList<WebSocketSession>> roompool= new HashMap<String,  ArrayList<WebSocketSession>>();     //roomid ,  [sessions]
    private static HashMap<WebSocketSession,String> sessionpool=new HashMap<WebSocketSession, String>();     //session , roomid

    private static final Gson gson = new GsonBuilder().create();

    @Override
    public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        
        String username;
        String roomid;
        String msg ;
        
        System.out.println("处理");

        final JsonObject jsonMessage = gson.fromJson(message.getPayload(), JsonObject.class);         //json字符串转对象      定义json：type，username ，roomid，msg

        switch (jsonMessage.get("type").getAsString()){


            case "log": {
                        System.out.println("登入了");
                        username = jsonMessage.get("username").getAsString();                  //登入

                        roomid = jsonMessage.get("roomid").getAsString();
                         sessionpool.put( session,roomid);
                        if(roompool.containsKey(roomid)){
                          ArrayList<WebSocketSession> sessions=roompool.get(roomid);
                            sessions.add( session);
                            roompool.replace(roomid,sessions);
                                }
                        else {
                            ArrayList<WebSocketSession> sessions=new ArrayList<WebSocketSession>();
                            sessions.add( session);
                            roompool.put(roomid,sessions);
                        }
                        System.out.println(username + "连接上了");
                        System.out.println(roompool+"..."+sessionpool);
                        break;
                        }

            case "chat":{
                        System.out.println("收到");
                        username = jsonMessage.get("username").getAsString();
                        roomid=sessionpool.get(session);
                        msg = jsonMessage.get("msg").getAsString();
                        SendMsg(roomid,msg,username);
                        break;
                         }

            default: break ;
        }





    }







    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        String roomid=sessionpool.get(session);

        sessionpool.remove(session);

        ArrayList<WebSocketSession> sessions=roompool.get(roomid);
        sessions.remove(session);
        roompool.replace(roomid,sessions);
        System.out.println("断开连接");
    }

    public void SendMsg(String roomid,String message,String username) throws IOException {       //发送
        ArrayList<WebSocketSession> sessions=roompool.get(roomid);
        for (WebSocketSession session:sessions
        ) {
            session.sendMessage(new TextMessage(username+":"+message));

        }

    }



}
